package com.apexsoft.monitor.front.console.sill.service;

import com.alibaba.fastjson.JSONObject;
import com.apexsoft.front.common.support.Result;
import com.apexsoft.front.common.utils.StringUtil;
import com.apexsoft.monitor.front.console.sill.dao.SillDao;
import com.apexsoft.monitor.front.console.sill.om.Sill;
import com.apexsoft.monitor.front.console.pageModel.BasePageModel;
import com.apexsoft.monitor.front.console.pageModel.SillModel;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service("sillService")
public class SillService {

    private static final Logger log = LogManager.getRootLogger();

    @Autowired
    @Qualifier("sillDao")
    private SillDao sillDao = null;

    public JSONObject listByPage(Pagination page, SillModel pageModel) throws Exception {

        EntityWrapper<Sill> entityWrapper = new EntityWrapper<>();
        if (StringUtils.isNotBlank(pageModel.getSillCode())) {
            entityWrapper.and("sill_code={0}", pageModel.getSillCode());
        }
        if (StringUtils.isNotBlank(pageModel.getSillName())) {
            entityWrapper.like("sill_name", pageModel.getSillName());
        }
        if(StringUtils.isNotBlank(pageModel.getSillValue())){
            entityWrapper.like("sill_value", pageModel.getSillValue());
        }
        if (pageModel.getSillStatus()!=null){
            entityWrapper.and("sill_status={0}",pageModel.getSillStatus());
        }
        List<Sill> list = sillDao.selectPage(page, entityWrapper);

        return Result.commonListResult(list);
    }

    public JSONObject add(HttpServletRequest request) {

        JSONObject result = new JSONObject();
        String sillName = request.getParameter("sillName");
        String sillCode = request.getParameter("sillCode");
        String sillValue = request.getParameter("sillValue");
        String sillDesc = request.getParameter("sillDesc");
        String sillStatus = request.getParameter("sillStatus");

        if (StringUtils.isNotBlank(sillName)
                && StringUtils.isNotBlank(sillCode)
                && StringUtils.isNotBlank(sillValue)
                && StringUtils.isNotBlank(sillStatus)
                ) {

            Sill sill = new Sill();
            sill.setSillCode(sillCode);
            sill.setSillName(sillName);
            sill.setSillDesc(sillDesc);
            sill.setSillValue(sillValue);
            sill.setSillStatus(StringUtils.isNumeric(sillStatus)?Integer.parseInt(sillStatus):0);
            try {
                Sill tempSill = sillDao.selectBySillCode(sillCode);
                if (tempSill!=null){
                    result.put(Result.CODE, Result.EC_1000);
                    result.put(Result.NOTE, "阀值代码["+sillCode+"]已存在");
                    return result;
                }
                long insertRst = sillDao.insert(sill);
                if (insertRst> Result.EC_1000) {
                    result.put(Result.CODE, Result.SC_0001);
                    result.put(Result.NOTE, "新增阀值成功");
                }
            } catch (Exception e) {
                log.error("新增阀值失败："+e.getMessage(), e);
                result.put(Result.CODE, Result.EC_1000);
                result.put(Result.NOTE, "新增阀值失败");
            }

        }else {
            result.put(Result.CODE, Result.EC_1000);
            result.put(Result.NOTE, "参数不正确");
        }
        return result;
    }

    public JSONObject update(HttpServletRequest request) {

        JSONObject result = new JSONObject();
        String id = request.getParameter("id");
        String sillName = request.getParameter("sillName");
        String sillCode = request.getParameter("sillCode");
        String sillValue = request.getParameter("sillValue");
        String sillDesc = request.getParameter("sillDesc");
        String sillStatus = request.getParameter("sillStatus");
        if (StringUtils.isNotBlank(sillName)
                && StringUtils.isNotBlank(sillCode)
                && StringUtils.isNotBlank(sillStatus)
                && StringUtils.isNotBlank(sillValue)
                ) {

            Sill sill = new Sill();
            sill.setId(Integer.parseInt(id));
            sill.setSillCode(sillCode);
            sill.setSillName(sillName);
            sill.setSillValue(sillValue);
            sill.setSillDesc(sillDesc);
            sill.setSillStatus(StringUtils.isNumeric(sillStatus)?Integer.parseInt(sillStatus):0);
            try {
                int uptRst = sillDao.updateSelectiveById(sill);
                if (uptRst> Result.EC_1000) {
                    result.put(Result.CODE, Result.SC_0001);
                    result.put(Result.NOTE, "修改阀值成功");
                }
            } catch (Exception e) {
                log.error("修改阀值失败："+e.getMessage(), e);
                result.put(Result.CODE, Result.EC_1000);
                result.put(Result.NOTE, "修改阀值失败");
            }

        }else {
            result.put(Result.CODE, Result.EC_1000);
            result.put(Result.NOTE, "参数不正确");
        }
        return result;
    }

    public JSONObject delete(String id) {

        JSONObject result = new JSONObject();
        if (StringUtils.isNotBlank(id)) {

            try {
                int delRst = sillDao.deleteById(id);
                if (delRst> Result.EC_1000) {
                    result.put(Result.CODE, Result.SC_0001);
                    result.put(Result.NOTE, "删除阀值成功");
                }
            } catch (Exception e) {
                log.error("删除阀值失败："+e.getMessage(), e);
                result.put(Result.CODE, Result.EC_1000);
                result.put(Result.NOTE, "删除阀值失败");
            }


        }else {
            result.put(Result.CODE, Result.EC_1000);
            result.put(Result.NOTE, "参数不正确");
        }
        return result;
    }

    public Sill getById(int id) {
        return sillDao.selectById(id);
    }
    public Sill getBySillCode(String code) {
        return sillDao.selectBySillCode(code);
    }

}


